/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.biblioteca.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.its.dsw2.biblioteca.modelo.Libro;

/**
 *
 * @author Maq-8
 */
@Stateless
public class LibroDao extends AbstractDao<Libro> implements LibroDaoLocal {
    @PersistenceContext(unitName = "Biblioteca8E-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public LibroDao() {
        super(Libro.class);
    }
     public boolean findDuplicity(Libro book){
    Query q=em.createNamedQuery("Libro.findByClave");
    q.setParameter("clave", book.getClave());
    return !q.getResultList().isEmpty();
    
    }   

    @Override
    public Libro findBookForLend(String claveLibro) {
    Query q=em.createNamedQuery("Libro.findByClave");
    q.setParameter("clave", claveLibro);
    if(!q.getResultList().isEmpty()){
    return (Libro)q.getResultList().get(0);
    }else{
    return null;
    }
    
    }

    @Override
    public Libro findByDuplicity(String claveLibro) {
            Query q=em.createNamedQuery("Libro.findByClaveEstado");
    q.setParameter("clave", claveLibro);
    q.setParameter("estado", claveLibro);
    if(!q.getResultList().isEmpty()){
    return (Libro)q.getResultList().get(0);
    }else{
    return null;
    }
    }
}